package cn.stylefeng.guns.modular.addressbook.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.addressbook.entity.AddressBook;
import cn.stylefeng.guns.modular.addressbook.enums.AddressBookExceptionEnum;
import cn.stylefeng.guns.modular.addressbook.mapper.AddressBookMapper;
import cn.stylefeng.guns.modular.addressbook.pojo.request.AddressBookRequest;
import cn.stylefeng.guns.modular.addressbook.service.AddressBookService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 业务实现层
 *
 * @author xbz
 * @date 2023/04/27 09:19
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

	@Override
    public void add(AddressBookRequest addressBookRequest) {
        AddressBook addressBook = new AddressBook();
        BeanUtil.copyProperties(addressBookRequest, addressBook);
        this.save(addressBook);
    }

    @Override
    public void del(AddressBookRequest addressBookRequest) {
        AddressBook addressBook = this.queryAddressBook(addressBookRequest);
        this.removeById(addressBook.getId());
    }

    @Override
    public void edit(AddressBookRequest addressBookRequest) {
        AddressBook addressBook = this.queryAddressBook(addressBookRequest);
        BeanUtil.copyProperties(addressBookRequest, addressBook);
        this.updateById(addressBook);
    }

    @Override
    public AddressBook detail(AddressBookRequest addressBookRequest) {
        return this.queryAddressBook(addressBookRequest);
    }

    @Override
    public PageResult<AddressBook> findPage(AddressBookRequest addressBookRequest) {
        LambdaQueryWrapper<AddressBook> wrapper = createWrapper(addressBookRequest);
        Page<AddressBook> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<AddressBook> findList(AddressBookRequest addressBookRequest) {
        LambdaQueryWrapper<AddressBook> wrapper = this.createWrapper(addressBookRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    private AddressBook queryAddressBook(AddressBookRequest addressBookRequest) {
        AddressBook addressBook = this.getById(addressBookRequest.getId());
        if (ObjectUtil.isEmpty(addressBook)) {
            throw new ServiceException(AddressBookExceptionEnum.ADDRESS_BOOK_NOT_EXISTED);
        }
        return addressBook;
    }

    /**
     * 创建查询wrapper
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    private LambdaQueryWrapper<AddressBook> createWrapper(AddressBookRequest addressBookRequest) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        Long id = addressBookRequest.getId();
        Long departmentId = addressBookRequest.getDepartmentId();
        String name = addressBookRequest.getName();
        Boolean sex = addressBookRequest.getSex();
        Long positionId = addressBookRequest.getPositionId();
        Boolean isEmergencyPerson = addressBookRequest.getIsEmergencyPerson();
        String officePhone = addressBookRequest.getOfficePhone();
        String phone = addressBookRequest.getPhone();
        String otherContact = addressBookRequest.getOtherContact();
        String email = addressBookRequest.getEmail();
        String delFlag = addressBookRequest.getDelFlag();

        queryWrapper.eq(ObjectUtil.isNotNull(id), AddressBook::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(departmentId), AddressBook::getDepartmentId, departmentId);
        queryWrapper.like(ObjectUtil.isNotEmpty(name), AddressBook::getName, name);
        queryWrapper.eq(ObjectUtil.isNotNull(sex), AddressBook::getSex, sex);
        queryWrapper.eq(ObjectUtil.isNotNull(positionId), AddressBook::getPositionId, positionId);
        queryWrapper.eq(ObjectUtil.isNotNull(isEmergencyPerson), AddressBook::getIsEmergencyPerson, isEmergencyPerson);
        queryWrapper.like(ObjectUtil.isNotEmpty(officePhone), AddressBook::getOfficePhone, officePhone);
        queryWrapper.like(ObjectUtil.isNotEmpty(phone), AddressBook::getPhone, phone);
        queryWrapper.like(ObjectUtil.isNotEmpty(otherContact), AddressBook::getOtherContact, otherContact);
        queryWrapper.like(ObjectUtil.isNotEmpty(email), AddressBook::getEmail, email);
        queryWrapper.like(ObjectUtil.isNotEmpty(delFlag), AddressBook::getDelFlag, delFlag);

        return queryWrapper;
    }

}